package com.likou.second_challenge;

public class q11 {

    public int maxArea(int[] height) {
        int n = height.length;
        int left = 0;
        int right = n - 1;

        int ans = 0;
        while (left <= right) {
            int l = height[left];
            int r = height[right];
            int h = Math.min(l, r);
            int d = right - left;
            int s = h * d;
            ans = Math.max(ans, s);

            if (l < r) {
                while (left <= right && height[left] <= h) {
                    left++;
                }
            } else {
                while (right >= left && height[right] <= h) {
                    right--;
                }
            }

        }

        return ans;
    }

}
